<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use api\index\controller\CommonController;
use app\admin\model\CommonModel;
use app\admin\model\VideoModel;
use cmf\controller\AdminBaseController;
use think\Db;

//视频管理
class VideoController extends AdminBaseController
{
    public function index(){
        $param = $this->request->param();
        $video_category = $this->getVideoCategory();
        $is_rotation = VideoModel::ROTATION;
        $list = Db::name('video')
            ->where(function($query) use($param){
                if(isset($param['title']) && !empty($param['title'])){
                    $title = $param['title'];
                    $query->where('title','like','%'.$title.'%');
                }
                if(isset($param['video_category_id']) && $param['video_category_id'] != '-1'){
                    $query->where('video_category_id',$param['video_category_id']);
                }
            })
            ->field('*')
            ->order('id desc')
            ->paginate(null,false,['query'=>$param])
            ->each(function($item, $key) use($video_category,$is_rotation){
                $item['video_url'] = CommonController::absolutionUrl($item['video_url']);
                $item['video_category_id'] = $video_category[$item['video_category_id']];
                $item['is_rotation'] = $is_rotation[$item['is_rotation']];
                return $item;
            });
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);

        //视频分类
        $this->assign('video_category',$this->getVideoCategory());
        //搜索视频分类回显
        $this->assign('video_category_id',$param['video_category_id']??-1);
        return $this->fetch();
    }

    //添加页面
    public function add(){
        //视频分类
        $this->assign('video_category',$this->getVideoCategory());
        //是否轮播
        $this->assign('is_rotation',VideoModel::ROTATION);
        //是否任务
        $this->assign('is_task',VideoModel::IS_TASK);
        return $this->fetch();
    }

    //添加提交
    public function addPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'Video.add');
        if($result !== true){
            $this->error($result);
        }
        if ($data['is_task']){
            VideoModel::where('is_task',true)->update(['is_task'=>false]);
        }
        //过滤data数组中的非数据表字段数据
        $videoModel = new VideoModel();
        $res = $videoModel->allowField(true)->create($data);
        if($res){
            $this->success('添加成功!', url('Video/index'));
        }else{
            $this->error('添加失败');
        }
    }

    //编辑页面
    public function edit(){
        $id = $this->request->param('id');
        $post = Db::name('video')
            ->where(['id'=>$id])
            ->find();
        $this->assign('post',$post);

        //视频分类
        $this->assign('video_category',$this->getVideoCategory());
        //是否轮播
        $this->assign('is_rotation',VideoModel::ROTATION);
        //是否每日任务
        $this->assign('is_task',VideoModel::IS_TASK);
        return $this->fetch();
    }

    //编辑提交
    public function editPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'Video.edit');
        if($result !== true){
            $this->error($result);
        }
        if ($data['is_task']){
            VideoModel::where('is_task',true)->update(['is_task'=>false]);
        }
        //过滤data数组中的非数据表字段数据
        $videoModel = new VideoModel();
        $res = $videoModel->allowField(true)->save($data,['id'=>$data['id']]);
        if($res){
            $this->success('保存成功!', url('Video/index'));
        }else{
            $this->error('保存失败');
        }
    }

    //删除
    public function delete(){
        $id = $this->request->param('id');
        $videoModel = new VideoModel();
        $res = $videoModel->where('id',$id)->delete();
        if($res){
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }

    //获取视频分类
    public function getVideoCategory(){
        $commonModel = new CommonModel();
        $res = $commonModel->selectData('video_category','id,video_name');
        $arr = [];
        foreach($res as $value){
            $arr[$value['id']] = $value['video_name'];
        }
        return $arr;
    }
}